import Vue from "vue";
import Router from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
NProgress.configure({ showSpinner: false })
Vue.use(Router);

const routes = [
  {
    path: "/demo",
    name: "demo",
    // component: () => import("@/views/demo.vue"),
    component: () => import("@/layout/index.vue"),
  },
  // 登录页
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login.vue"),
  },
  // 首页
  {
    path: "/",
    meta: {
      title: "首页",
    },
    component: () => import("@/views/index.vue"),
    redirect: "/index",
    children: [
      // 首页
      {
        path: "/index",
        name: "index",
        component: () => import("@/views/index/index.vue"),
      },
      // 系统工具
      {
        path: "/tool",
        name: "tool",
        meta: {
          title: "工具",
        },
        redirect: "/tool/generateCode",
        component: () => import("@/views/tool/index.vue"),
        children: [
          {
            path: "/tool/buildForm",
            name: "buildForm",
            meta: {
              title: "表单构建",
            },
            component: () => import("@/views/tool/buildForm.vue"),
          },
          {
            path: "/tool/generateCode",
            name: "generateCode",
            meta: {
              title: "代码生成",
            },
            component: () => import("@/views/tool/generate/index.vue"),
          },
          {
            path: "/tool/buildDatabase",
            name: "buildDatabase",
            meta: {
              title: "基础配置",
            },
            component: () => import("@/views/tool/baseConfig.vue"),
          },
        ],
      },
    ],
  },
];

// 防止连续点击多次路由报错
let routerPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((err) => err);
};

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  NProgress.start();
  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
